
import { Controller, Inject, Get, Query, Param, Del, Post, Put, Body } from '@midwayjs/core';
import { Auth } from "../decorator/auth.decorator";
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@midwayjs/swagger';
import { BusinessType, Log } from "../decorator/log.decorator";
import { GroupService } from '@service/group.service';
import { CreateGroupDto, UpdateGroupDto, QueryGroupDto, QueryGroupStudentDto } from '@dto/group.dto';

@ApiTags('分组信息')
@Controller('system/group')
export class GroupController {
  @Inject()
  groupService: GroupService;
  // 创建
  @ApiOperation({ summary: '创建分组信息', description: '创建新的分组信息记录' })
  @ApiBody({ type: CreateGroupDto, description: '分组信息信息数据' })
  @ApiResponse({ status: 200, description: '创建成功' })
  @Auth('system:group:add')
  @Log({ title: '分组信息-创建', businessType: BusinessType.ADD })
  @Post('/')
  async create(@Body() body: CreateGroupDto) {
    return await this.groupService.create(body);
  }
  // 获取列表
  @ApiOperation({ summary: '获取分组信息列表', description: '分页查询分组信息信息列表' })
  @ApiResponse({ status: 200, description: '查询成功' })
  @Auth('system:group:query')
  @Log({ title: '分组信息-列表', businessType: BusinessType.LIST })
  @Get('/list')
  async list(@Query() query: QueryGroupDto) {
    return await this.groupService.list(query);
  }
  // 获取列表（分配）
  @ApiOperation({ summary: '获取分组信息列表（分配）', description: '分页查询分组信息信息列表（分配）' })
  @ApiResponse({ status: 200, description: '查询成功' })
  @Auth('system:group:query')
  @Log({ title: '分组信息-列表（分配）', businessType: BusinessType.LIST })
  @Get('/listForAllocate')
  async listForAllocate(@Query() query: QueryGroupDto) {
    const now = new Date();
    query.endTime = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}:${String(now.getSeconds()).padStart(2, '0')}`;
    return await this.groupService.list(query);
  }

  // 根据年级ID获取分组列表
  @ApiOperation({ summary: '根据年级ID获取分组列表', description: '根据年级ID查询该年级下的所有分组' })
  @ApiParam({ name: 'gradeId', description: '年级ID', type: 'number' })
  @ApiResponse({ status: 200, description: '查询成功' })
  @Log({ title: '分组信息-按年级查询', businessType: BusinessType.LIST })
  @Get('/listByGrade/:gradeId')
  async listByGrade(@Param('gradeId') gradeId: string) {
    return await this.groupService.listByGrade(+gradeId);
  }
    // 根据年级ID获取分组列表
  @ApiOperation({ summary: '根据年级ID获取分组列表', description: '根据年级ID查询该年级下的所有分组' })
  @ApiParam({ name: 'gradeId', description: '年级ID', type: 'number' })
  @ApiResponse({ status: 200, description: '查询成功' })
  @Log({ title: '分组信息-按年级查询', businessType: BusinessType.LIST })
  @Get('/listByGradeAndTeacher/:gradeId/teacher/:teacherId')
  async listByGradeAndTeacher(@Param('gradeId') gradeId: string, @Param('teacherId') teacherId: string) {
    return await this.groupService.listByGradeAndTeacher(+gradeId, +teacherId);
  }
  // 获取详情
  @ApiOperation({ summary: '获取分组信息详情', description: '根据groupId查询分组信息详细信息' })
  @ApiParam({ name: 'groupId', description: 'groupId值' })
  @ApiResponse({ status: 200, description: '查询成功' })
  @Log({ title: '分组信息-详情', businessType: BusinessType.DETAIL })
  @Get('/:groupId')
  async detail(@Param('groupId') groupId: string) {
    return await this.groupService.detail(groupId);
  }
  // 修改
  @ApiOperation({ summary: '修改分组信息', description: '根据groupId修改分组信息信息' })
  @ApiBody({ type: UpdateGroupDto, description: '更新的分组信息信息数据' })
  @ApiResponse({ status: 200, description: '修改成功' })
  @Auth('system:group:edit')
  @Log({ title: '分组信息-修改', businessType: BusinessType.EDIT })
  @Put('/')
  async update(@Body() body: UpdateGroupDto) {
    return await this.groupService.update(body);
  }
  // 删除
  @ApiOperation({ summary: '删除分组信息', description: '根据groupId删除分组信息记录' })
  @ApiParam({ name: 'groupId', description: 'groupId值' })
  @ApiResponse({ status: 200, description: '删除成功' })
  @Auth('system:group:remove')
  @Log({ title: '分组信息-删除', businessType: BusinessType.REMOVE })  
  @Del('/:groupId')
  async remove(@Param('groupId') groupId: string) {
    const groupIds = groupId.split(',').map((groupId) => +groupId);
    return this.groupService.remove(groupIds);
  }
  // 导出
  @ApiOperation({ summary: '导出分组信息', description: '根据查询参数导出分组信息数据' })
  @ApiBody({ type: QueryGroupDto, description: '导出查询参数' })
  @ApiResponse({ status: 200, description: '导出成功' })
  @Auth('system:group:export')
  @Log({ title: '分组信息-导出', businessType: BusinessType.EXPORT })
  @Post('/export')
  async export(@Body() queryParams: QueryGroupDto) {
    return await this.groupService.export(queryParams);
  }
  @ApiOperation({ summary: '获取分组学生信息详情', description: '根据groupId查询分组学生信息详细信息' })
  @ApiParam({ name: 'groupId', description: 'groupId值' })
  @ApiResponse({ status: 200, description: '查询成功' })
  @Log({ title: '分组学生信息-详情', businessType: BusinessType.DETAIL })
  @Get('/:groupId/student/list')
  async detailStudent(@Param('groupId') groupId: number, @Query() query: QueryGroupStudentDto) {
    console.log(groupId, query);
    return await this.groupService.detailStudent(groupId, query);
  }

  @ApiOperation({ summary: '获取分组教师成员信息', description: '根据groupId查询分组下的教师信息（分页）' })
  @ApiParam({ name: 'groupId', description: 'groupId值' })
  @ApiResponse({ status: 200, description: '查询成功' })
  @Log({ title: '分组成员信息-详情', businessType: BusinessType.DETAIL })
  @Get('/:groupId/teacher/list')
  async detailTeacher(@Param('groupId') groupId: string) {
    return await this.groupService.detailTeacher(+groupId);
  }
  
}